
import { DeterminedPlan, GroupStructure, PlanedStructure, RelevantPlan } from "./structure";

export class StructureFixed extends GroupStructure{
    /**
     * 
     * @param {Room} spawnRoom 
     * @param {RoomPosition} rootPos 
     * @param {RelevantPlan} map 
     */
    constructor(spawnRoom = undefined, rootPos = undefined, map = undefined){
        super(spawnRoom, rootPos);
        if(spawnRoom !== undefined){
            this.structureList = [];
            map.check(rootPos,(type, x, y)=>{
                this.structureList.push(new PlanedStructure({x:x,y:y,roomName:rootPos.roomName},type));
            });
            this.map = map.getPlanList(rootPos);
        }
    }
    serialize(){
        let list = [];
        for(let plan of this.structureList){//todo:
            if(!(plan instanceof PlanedStructure)){
                continue;
            }
            list.push(plan.serialize());
        }
        return {
            super: super.serialize(),
            map: this.map.serialize(),
            structureList: list,
        }
    }
    deserialize(obj){
        super.deserialize(obj.super);
        this.map = new DeterminedPlan().deserialize(obj.map);
        this.structureList = [];
        for(let _obj of obj.structureList){
            this.structureList.push(new PlanedStructure().deserialize(_obj));
        }//todo: check any grammar err
        return this;
    }
    plan(){
        return this.map
            .getPlanList(this.rootPos)
            .buildAll();
    }
    run(){
        if(!this.map.isDone()){
            return this.map.buildAll();
        }
    }
}